package com.sh.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.springframework.web.multipart.MultipartFile;
public class FileUitl {

	/**
	 * 创建文件 并且 返回 创建文件的 服务器真实路径
	 * @throws Exception
	 * */
	public static String createFile(MultipartFile stoneFile,String serverPath) throws IOException{

		File filePath = new File(serverPath);
		if(!filePath.exists()){
			filePath.mkdirs();
		}

		String path = serverPath + File.separator+stoneFile.getOriginalFilename();
		File file = new File(path);

		InputStream ins;

		ins = stoneFile.getInputStream();
		OutputStream os = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		os.close();

		return path;

	}

	public static String json2String(String path) throws IOException {
		StringBuilder result = new StringBuilder();
		InputStream in = new FileInputStream(path);
		//读取文件上的数据。
		// 将字节流向字符流的转换。
		InputStreamReader isr = new InputStreamReader(in,"UTF-8");//读取
		//创建字符流缓冲区
		BufferedReader bufr = new BufferedReader(isr);//缓冲

		String line = null;
		while ((line = bufr.readLine()) != null) {
			result.append(System.lineSeparator()+line);
		}
		isr.close();
		return result.toString();
	}
	public static void main(String[] args){
		String str;
		try {
			str = json2String("C:\\Users\\Administrator\\Desktop\\临时文件\\navMenus.json");
			System.out.print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


}
